#include "widget_game_end.h"
#include "widget_ai_statistics.h"
#include "widget_score_graph.h"
#include "widget_setup_testing.h"
#include "widget_setup_network.h"




game_end_widget::game_end_widget( player* pl, QWidget *parent, Qt::WFlags flags ): QDialog( parent, flags )
{
	this->setModal( true );
	this->setWindowTitle( "AI Tester - Game end" );
	QVBoxLayout* layout = new QVBoxLayout( this );


	QLabel* title_l = new QLabel( this );
	title_l->setTextFormat( Qt::RichText );
	ostringstream sout;
	sout << "<b><font size=7>GAME END</font></b>";
	title_l->setText( sout.str().c_str() );
	title_l->setAlignment( Qt::AlignHCenter );
	sout.str( "" );
	layout->addWidget( title_l );


	layout->addSpacing( 20 );


	QLabel* winner_l = new QLabel( this );
	winner_l->setTextFormat( Qt::RichText );
	sout << "<font size=5>" << pl->get_name() << " (player " << pl->get_id() << ") won!</font>";
	winner_l->setText( sout.str().c_str() );
	layout->addWidget( winner_l );


	layout->addSpacing( 20 );


	QWidget* info_buttons_w = new QWidget( this );
	QHBoxLayout* info_buttons_w_l = new QHBoxLayout( info_buttons_w );

	QPushButton* show_stat_b = new QPushButton( "Show statistics", info_buttons_w );
	info_buttons_w_l->addWidget( show_stat_b );
	show_stat_b->setEnabled( !( game::get_instance()->is_replay() ) );
	connect( show_stat_b, SIGNAL( clicked() ), this, SLOT( show_statistics() ) );

	QPushButton* export_stat_b = new QPushButton( "Export statistics data", info_buttons_w );
	info_buttons_w_l->addWidget( export_stat_b );
	export_stat_b->setEnabled( !( game::get_instance()->is_replay() ) );
	connect( export_stat_b, SIGNAL( clicked() ), this, SLOT( export_statistics() ) );

	QPushButton* show_graph_b = new QPushButton( "Show progress graph", info_buttons_w );
	info_buttons_w_l->addWidget( show_graph_b );
	connect( show_graph_b, SIGNAL( clicked() ), this, SLOT( show_graph() ) );

	QPushButton* export_progress_b = new QPushButton( "Export progress data", info_buttons_w );
	info_buttons_w_l->addWidget( export_progress_b );
	connect( export_progress_b, SIGNAL( clicked() ), this, SLOT( export_progress() ) );
	
	info_buttons_w->setLayout( info_buttons_w_l );
	layout->addWidget( info_buttons_w );


	QWidget* save_buttons_w = new QWidget( this );
	QHBoxLayout* save_buttons_w_l = new QHBoxLayout( save_buttons_w );

	QPushButton* save_objects_b = new QPushButton( "Save starting objects", save_buttons_w );
	save_buttons_w_l->addWidget( save_objects_b );
	save_objects_b->setEnabled( !( game::get_instance()->is_replay() ) );
	connect( save_objects_b, SIGNAL( clicked() ), this, SLOT( save_objects() ) );

	QPushButton* save_replay_b = new QPushButton( "Save commands", save_buttons_w );
	save_buttons_w_l->addWidget( save_replay_b );
	save_replay_b->setEnabled( !( game::get_instance()->is_replay() ) );
	connect( save_replay_b, SIGNAL( clicked() ), this, SLOT( save_replay() ) );
	
	save_buttons_w->setLayout( save_buttons_w_l );
	layout->addWidget( save_buttons_w );


	layout->addSpacing( 10 );


	QWidget* end_buttons_w = new QWidget( this );
	QHBoxLayout* end_buttons_w_l = new QHBoxLayout( end_buttons_w );

	QPushButton* new_game_b = new QPushButton( "New game", end_buttons_w );
	end_buttons_w_l->addWidget( new_game_b );
	new_game_b->setEnabled( !( game::get_instance()->is_replay() ) );
	connect( new_game_b, SIGNAL( clicked() ), this, SLOT( new_game() ) );

	QPushButton* exit_to_menu_b = new QPushButton( "Exit to main menu", end_buttons_w );
	end_buttons_w_l->addWidget( exit_to_menu_b );
	connect( exit_to_menu_b, SIGNAL( clicked() ), this, SLOT( exit_to_menu() ) );

	QPushButton* exit_b = new QPushButton( "Exit", end_buttons_w );
	end_buttons_w_l->addWidget( exit_b );
	connect( exit_b, SIGNAL( clicked() ), this, SLOT( exit_app() ) );
	
	end_buttons_w->setLayout( end_buttons_w_l );
	layout->addWidget( end_buttons_w );

	
	this->setLayout( layout );
	//this->setFixedSize( 442, 224 );
};



void game_end_widget::show_statistics()
{
	QMainWindow* wasw = new QMainWindow( this );
	QScrollArea* sa = new QScrollArea( wasw );
	ai_statistics_widget* asw = new ai_statistics_widget( sa );
	sa->setWidget( asw );
	sa->setWidgetResizable( true );
	wasw->setCentralWidget( sa );
	wasw->resize( 600, 300 );
	wasw->show();
};



void game_end_widget::show_graph()
{
	QMainWindow* wsgw = new QMainWindow( this );
	wsgw->setCentralWidget( new score_graph_widget( game::get_instance()->get_score_history(), wsgw ) );
	wsgw->resize( 600, 250 );
	wsgw->show();
};



void game_end_widget::export_statistics()
{
	ofstream fout;
	fout.open( QFileDialog::getSaveFileName( this, "Export AI statistics data", "./", "Text files (*.txt)" ).toStdString().c_str(), ios::trunc );
	if ( fout.good() )
	{
		for ( unsigned int p = 0; p < players::get_instance()->get_players_count(); ++p )
		{
			player* pl = players::get_instance()->get_player( p );
			if ( pl->get_type() == ai )
			{
				fout << "Player " << p << endl;
				vector< double >* data = &( pl->ai_algorithm->time_cons );
				for ( unsigned int i = 0; i < data->size(); ++i )
				{
					fout << i << "\t" << ( *data )[ i ] << endl;
				}
				fout << endl;
			}
		}
		fout.close();
		cout << "AI STATISTICS EXPORTED SUCCESSFULLY" << endl;
	}
	else
	{
		cout << "ERROR WHILE EXPORTING AI STATISTICS" << endl;
		fout.close();
	}
};



void game_end_widget::export_progress()
{
	ofstream fout;
	fout.open( QFileDialog::getSaveFileName( this, "Export game progress data", "./", "Text files (*.txt)" ).toStdString().c_str(), ios::trunc );
	if ( fout.good() )
	{
		vector< vector< double > >* data = game::get_instance()->get_score_history();
		for ( unsigned int i = 0; i < ( *data )[ 0 ].size(); ++i )
		{
			fout << i;
			for ( unsigned int p = 0; p < data->size(); ++p )
			{
				 fout << "\t" << ( *data )[ p ][ i ];
			}
			fout << endl;
		};
		fout.close();
		cout << "GAME PROGRESS EXPORTED SUCCESSFULLY" << endl;
	}
	else
	{
		cout << "ERROR WHILE EXPORTING GAME PROGRESS" << endl;
		fout.close();
	}
};



void game_end_widget::save_objects()
{
	game::get_instance()->export_starting_objects( QFileDialog::getSaveFileName( this, "Save starting objects", "./objects", "Objects files (*.obj)" ).toStdString().c_str() );
};



void game_end_widget::save_replay()
{
	game::get_instance()->export_players_commands( QFileDialog::getSaveFileName( this, "Save commands", "./replays", "Commands files (*.rpl)" ).toStdString().c_str() );
};



void game_end_widget::new_game()
{
	this->close();
	if ( game::get_instance()->is_in_testing_mode() )
	{		
		players::get_instance()->rebuild();
		game::get_instance()->rebuild();
		ai_algorithms::get_instance()->rebuild();
		mapa::get_instance()->rebuild();
		players::get_instance()->fill_players();

		game_gui* ggui = game_gui::get_instance();

		ggui->g->showNormal();
		QScrollArea* sa = new QScrollArea( ggui->g );
		ggui->g->setup_w = new testing_setup_widget( sa );
		sa->setWidget( ggui->g->setup_w );
		sa->setWidgetResizable( true );
		ggui->g->play_w->deleteLater();
		ggui->g->play_w = 0;
		ggui->g->setCentralWidget( sa );
		ggui->g->setMinimumSize( 751, 441 );
		ggui->g->resize( 751, 441 );
	}
	else if ( game::get_instance()->is_network_game() )
	{
		network_client::get_instance()->rebuild();
		players::get_instance()->rebuild();
		game::get_instance()->rebuild();
		ai_algorithms::get_instance()->rebuild();
		mapa::get_instance()->rebuild();
		players::get_instance()->fill_players();

		game_gui* ggui = game_gui::get_instance();

		ggui->g->showNormal();
		QScrollArea* sa = new QScrollArea( ggui->g );
		ggui->g->setup_w = new network_setup_widget( sa );
		sa->setWidget( ggui->g->setup_w );
		sa->setWidgetResizable( true );
		ggui->g->play_w->deleteLater();
		ggui->g->play_w = 0;
		ggui->g->setCentralWidget( sa );
		ggui->g->setMinimumSize( 665, 339 );
		ggui->g->resize( 665, 339 );
	}
	else
	{
		mapa::get_instance()->clean_map();
		players::get_instance()->clean_players();

		istringstream start_obj_str( game::get_instance()->starting_objects );
		mapa::get_instance()->load_objects( start_obj_str );

		game::get_instance()->rebuild();

		game_gui* ggui = game_gui::get_instance();
		ggui->g->showNormal();
		ggui->g->setMinimumSize( 620, 486 );
		ggui->g->resize( 620, 486 );	
		ggui->start_buy();
	}
};



void game_end_widget::exit_to_menu()
{
	this->close();

	if ( game::get_instance()->is_network_game() )
		network_client::get_instance()->rebuild();
	players::get_instance()->rebuild();
	game::get_instance()->rebuild();
	ai_algorithms::get_instance()->rebuild();
	mapa::get_instance()->rebuild();
	players::get_instance()->fill_players();

	game_gui* ggui = game_gui::get_instance();

	ggui->g->showNormal();
	QScrollArea* sa = new QScrollArea( ggui->g );
	ggui->g->main_w = new main_widget( sa );
	ggui->g->setCentralWidget( ggui->g->main_w );
	sa->setWidget( ggui->g->main_w );
	sa->setWidgetResizable( true );
	ggui->g->play_w->deleteLater();
	ggui->g->play_w = 0;
	ggui->g->setCentralWidget( sa );
	ggui->g->setMinimumSize( 210, 329 );
	ggui->g->resize( 210, 329 );	
};



void game_end_widget::exit_app()
{
	qApp->quit();
};